package
{
	import flash.display.MovieClip;

	public class FloorGuardObject
	{
		public var name:String;
		public var pos:Vector2;
		public var r:Number;
		public var dir:Number;
		public var speed:Number;
		public var view:Vector2;
		public var mc:MovieClip;

		public function FloorGuardObject()
		{
			this.name = "floor guard";
			this.pos = new Vector2(41, 14);
			this.r = Main.getInstance().tiles.xw * 0.5;
			this.dir = 1;
			this.speed = Main.getInstance().tiles.xw * 0.428571428571429;
			this.view = new Vector2(0, 0);
			Main.getInstance().objects.Register(this);
			this.mc = Main.getInstance().gfx.CreateSprite("debugFloorGuardMC", NinjaGraphicsSystem.LAYER_OBJECTS);
			this.mc.visible = false;
			this.mc.gotoAndStop("floorguard_idle");
		}

		public function Destruct():void
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
		}

		public var Update:Function;
		public var cell:TileMapCell;
		public var maxX:Number;
		public var minX:Number;
		public var mini:Number;
		public var maxi:Number;

		public function Init(params):void
		{
			var _local3 = params;
			if (_local3.length == 3)
			{
				this.pos.x = _local3[0];
				this.pos.y = _local3[1];
				if (dir < 0)
				{
					this.dir = -1;
				}
				else
				{
					this.dir = 1;
				}
				Main.getInstance().objects.AddToGrid(this);
				Main.getInstance().objects.Moved(this);
				Main.getInstance().objects.StartUpdate(this);
				this.Update = this.Update_Idle;
				this.pos.y = (this.cell.pos.y + this.cell.yw) - this.r;
				var _local2 = this.cell;
				while (true)
				{
					_local2 = _local2.nR;
					if ((TileMapCell.TID_EMPTY < _local2.ID) || (_local2.eD != TileMapCell.EID_SOLID))
					{
						this.maxX = (_local2.pos.x - _local2.xw) - this.r;
						break;
					}
				}
				while (true)
				{
					_local2 = _local2.nL;
					if ((TileMapCell.TID_EMPTY < _local2.ID) || (_local2.eD != TileMapCell.EID_SOLID))
					{
						this.minX = (_local2.pos.x + _local2.xw) + this.r;
						break;
					}
				}
				_local2 = this.cell;
				this.mini = _local2.i;
				this.maxi = _local2.i;
				while (true)
				{
					_local2 = _local2.nR;
					if (TileMapCell.TID_EMPTY < _local2.ID)
					{
						break;
					}
					this.maxi++;
				}
				_local2 = this.cell;
				while (true)
				{
					_local2 = _local2.nL;
					if (TileMapCell.TID_EMPTY < _local2.ID)
					{
						break;
					}
					this.mini--;
				}
				this.mc.scaleX = (this.mc.scaleY = 2 * this.r);
				this.Draw();
				this.mc.visible = true;
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndDraw(this);
		}

		public function DumpInitData():String
		{
			return "" + this.pos.x + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.pos.y + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.dir;
		}

		public function IdleAfterDeath():void
		{
			this.StopChasing();
			Main.getInstance().objects.EndUpdate(this);
		}

		public function Draw():void
		{
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
		}

		public function TestVsPlayer(guy):void
		{
			var p = guy.pos;
			var _local2 = this.pos.x - p.x;
			var _local1 = this.pos.y - p.y;
			var len = Math.sqrt((_local2 * _local2) + (_local1 * _local1));
			if (len < (this.r + guy.r))
			{
				_local2 = _local2 / len;
				_local1 = _local1 / len;
				Main.getInstance().particles.SpawnZap(this.pos.x - (_local2 * this.r), this.pos.y - (_local1 * this.r), Main.getInstance().NormToRot(-_local2, -_local1));
				Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_ELECTRIC, (-_local2) * 10, (-_local1) * 10, p.x + (guy.r * _local2), p.y + (guy.r * _local1), this);
			}
		}

		public function StartChasing():void
		{
			this.Update = this.Update_Chase;
			Main.getInstance().objects.StartDraw(this);
			this.mc.gotoAndStop("floorguard_active");
			if (PlayerObject.player.cell.i < this.cell.i)
			{
				this.dir = -1;
			}
			else if (this.cell.i < PlayerObject.player.cell.i)
			{
				this.dir = 1;
			}
			else
			{
				this.StopChasing();
			}
		}

		public function StopChasing():void
		{
			this.mc.gotoAndStop("floorguard_idle");
			this.Update = this.Update_Idle;
			Main.getInstance().objects.EndDraw(this);
		}

		public function Update_Idle():void
		{
			if (Math.abs(this.cell.j - PlayerObject.player.cell.j) == 0)
			{
				var _local1 = PlayerObject.player.cell.i;
				if ((this.maxi < _local1) || (_local1 < this.mini))
				{
				}
				else
				{
					this.StartChasing();
				}
			}
		}

		public function Update_Chase():void
		{
			if (this.dir < 0)
			{
				if (Math.abs(this.pos.x - this.minX) < this.speed)
				{
					this.pos.x = this.minX;
					this.StopChasing();
				}
				else
				{
					this.pos.x = this.pos.x + (this.dir * this.speed);
				}
			}
			else if (Math.abs(this.maxX - this.pos.x) < this.speed)
			{
				this.pos.x = this.maxX;
				this.StopChasing();
			}
			else
			{
				this.pos.x = this.pos.x + (this.dir * this.speed);
			}
			Main.getInstance().objects.Moved(this);
		}
	}
}